package com.xzj.admin.resource.analysis.handle.async;

import cn.hutool.core.util.StrUtil;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.resource.analysis.mapper.ResMessageManageMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: AsyncToPgHandle
 * @description: 异步操作PG库处理器
 * @author: XiaZunJun
 * @create: 2024-11-20 16:05
 */
@Component
@Slf4j
public class AsyncToPgHandle {

    @Resource
    private ResMessageManageMapper resMessageManageMapper;

    @Async("executorTask")
    public void batchAddOrUpdate(List<ResMessageManage> middleMessages, String cmdbType){
        if(CommonEnums.MESSAGE_TAG.add.equals(cmdbType)){
            resMessageManageMapper.batchInsertMessage(middleMessages);
        }else{
            String middleIds = "";
            for(ResMessageManage middleMessage:middleMessages){
                if(StrUtil.isNotEmpty(middleIds)){
                    middleIds += ",";
                }
                middleIds += middleMessage.getMessageId();
            }
            resMessageManageMapper.batchUpdateEdit(middleMessages,middleMessages.get(0),middleIds);
        }
    }

}
